package com.tang.user.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tang.common.entity.user.Admin;
import com.tang.common.exception.RecruitException;
import com.tang.common.exception.RecruitExceptionEnum;
import com.tang.user.mapper.AdminMapper;

import com.tang.user.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 唐朝阳
 */

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Admin find(String adminName, String password) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("admin_name", adminName);
        param.put("password", password);
        param.put("delete_flag",1);
        List<Admin> admins = adminMapper.selectByMap(param);
        if (!admins.isEmpty()) {
            return admins.get(0);
        }
        Admin admin = selectByAdminName(adminName);
        if(admin!=null){
            throw new RecruitException(RecruitExceptionEnum.PASSWORD_FAILED);
        }
        return null;
    }

    @Override
    public PageInfo<Admin> list(String keywords,Integer currentPage,Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        if (keywords == null || "".equals(keywords)) {
            QueryWrapper<Admin> qw = new QueryWrapper<Admin>();
            qw.eq("delete_flag",1);
            return PageInfo.of(adminMapper.selectList(qw));
        }
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<Admin>();
        queryWrapper.eq("delete_flag",1).and(q->q.like("admin_name",keywords).or().like("real_name",keywords));
        List<Admin> admins = adminMapper.selectList(queryWrapper);
        return  PageInfo.of(admins);
    }
//软删除,修改标志位为0
    @Override
    public int deleteByAdminId(int adminId) {
        Admin admin = adminMapper.selectById(adminId);
        admin.setDeleteFlag(0);
        return adminMapper.updateById(admin);
    }

    @Override
    public int enableDisableByAdminId(int adminId) {
        Admin admin = adminMapper.selectById(adminId);
        int result=0;
        if(admin!=null&&admin.getDeleteFlag()!=0) {
            admin.setState(admin.getState() == 1 ? 0 : 1);
            admin.setUpdateTime(new Date());
            result = adminMapper.updateById(admin);
        }
        return result;
    }

    @Override
    public int deleteByAdminName(String adminName) {
        Admin admin = selectByAdminName(adminName);
        if(admin!=null){
            admin.setDeleteFlag(0);
        }
        int result = adminMapper.updateById(admin);
        return result;
    }

    @Override
    public Admin selectByAdminName(String adminName) {
        Map<String,Object> map = new HashMap<>();
        map.put("admin_name",adminName);
        map.put("delete_flag",1);
        List<Admin> list = adminMapper.selectByMap(map);
        if(list.size() > 0) {
            return list.get(0);
        }
         return null;
    }

    @Override
    public int updateAdmin(Admin admin) {
        QueryWrapper<Admin> qw = new QueryWrapper<Admin>();
        qw.ne("admin_name",admin.getAdminName()).and(q->q.eq("email",admin.getEmail()).or().eq("tel",admin.getTel()));
        Admin admin1 = adminMapper.selectOne(qw);
        if(admin1!=null&&admin1.getDeleteFlag()==0){
            adminMapper.deleteById(admin1.getAdminId());
        }
        int result = adminMapper.updateById(admin);
        return result;
    }
//添加前查找数据库中是否有标志位为0的相同账号名数据，有就删除
    @Override
    public int addAdmin(Admin admin) {
        QueryWrapper<Admin> qw = new QueryWrapper<Admin>();
        qw.eq("admin_name",admin.getAdminName()).or().eq("tel",admin.getTel());
        Admin admin1 = adminMapper.selectOne(qw);
        if(admin1!=null&&admin1.getDeleteFlag()==0){
            adminMapper.deleteById(admin1.getAdminId());
        }
        int result = adminMapper.insert(admin);
        return result;
    }

    @Override
    public Admin selectByTel(String tel) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("tel",tel);
        map.put("delete_flag",1);
        List<Admin> list = adminMapper.selectByMap(map);
        if(list.size() > 0) {
            return list.get(0);
        }
        return null;
    }


    @Override
    public Admin selectByEmail(String email) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("email",email);
        map.put("delete_flag",1);
        List<Admin> list = adminMapper.selectByMap(map);
        if(list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public Integer getAdminCount() {
        return adminMapper.selectCount(null);
    }
}
